#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include <wiringPi.h>
#include "tft.h"

static unsigned char F8X16_ASCII[][16]={
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x00,0x00,0x00,0x00},
    {0x00,0x10,0x0C,0x02,0x10,0x0C,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x40,0xC0,0x78,0x40,0xC0,0x78,0x00,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x00},
    {0x00,0x70,0x88,0x88,0xFC,0x08,0x30,0x00,0x00,0x18,0x20,0x20,0xFF,0x21,0x1E,0x00},
    {0xF0,0x08,0xF0,0x80,0x60,0x18,0x00,0x00,0x00,0x31,0x0C,0x03,0x1E,0x21,0x1E,0x00},
    {0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x2C,0x19,0x27,0x21,0x10},
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x90,0x70,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00},
    {0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00},
    {0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00},
    {0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x0F,0x01,0x01,0x01},
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x90,0x70,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0xC0,0x38,0x04,0x00,0x00,0x60,0x18,0x07,0x00,0x00,0x00,0x00},
    {0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00},
    {0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00},
    {0x00,0x70,0x08,0x08,0x08,0x08,0xF0,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00},
    {0x00,0x30,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x18,0x20,0x21,0x21,0x22,0x1C,0x00},
    {0x00,0x00,0x80,0x40,0x30,0xF8,0x00,0x00,0x00,0x06,0x05,0x24,0x24,0x3F,0x24,0x24},
    {0x00,0xF8,0x88,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x20,0x20,0x20,0x11,0x0E,0x00},
    {0x00,0xE0,0x10,0x88,0x88,0x90,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x20,0x1F,0x00},
    {0x00,0x18,0x08,0x08,0x88,0x68,0x18,0x00,0x00,0x00,0x00,0x3E,0x01,0x00,0x00,0x00},
    {0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00},
    {0x00,0xF0,0x08,0x08,0x08,0x10,0xE0,0x00,0x00,0x01,0x12,0x22,0x22,0x11,0x0F,0x00},
    {0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00},
    {0x00,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x00,0x02,0x02,0x02,0x02,0x02,0x02,0x00},
    {0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00},
    {0x00,0x70,0x48,0x08,0x08,0x88,0x70,0x00,0x00,0x00,0x00,0x30,0x37,0x00,0x00,0x00},
    {0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x28,0x2F,0x28,0x17,0x00},
    {0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20},
    {0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00},
    {0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00},
    {0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00},
    {0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00},
    {0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00},
    {0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00},
    {0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20},
    {0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00},
    {0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00},
    {0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00},
    {0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00},
    {0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x01,0x3E,0x01,0x3F,0x20,0x00},
    {0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00},
    {0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00},
    {0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00},
    {0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x28,0x28,0x30,0x50,0x4F,0x00},
    {0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20},
    {0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00},
    {0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00},
    {0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00},
    {0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00},
    {0x08,0xF8,0x00,0xF8,0x00,0xF8,0x08,0x00,0x00,0x03,0x3E,0x01,0x3E,0x03,0x00,0x00},
    {0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20},
    {0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00},
    {0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00},
    {0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00},
    {0x00,0x00,0x00,0x00,0xC0,0x38,0x04,0x00,0x00,0x60,0x18,0x07,0x00,0x00,0x00,0x00},
    {0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00},
    {0x00,0x00,0x04,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80},
    {0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x19,0x24,0x24,0x12,0x3F,0x20,0x00},
    {0x10,0xF0,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00},
    {0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00},
    {0x00,0x00,0x80,0x80,0x80,0x90,0xF0,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20},
    {0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x24,0x24,0x24,0x24,0x17,0x00},
    {0x00,0x80,0x80,0xE0,0x90,0x90,0x20,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00},
    {0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00},
    {0x10,0xF0,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20},
    {0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00},
    {0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00},
    {0x10,0xF0,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x06,0x29,0x30,0x20,0x00},
    {0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00},
    {0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F},
    {0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20},
    {0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00},
    {0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0x91,0x20,0x20,0x11,0x0E,0x00},
    {0x00,0x00,0x00,0x80,0x80,0x00,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0x91,0xFF,0x80},
    {0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00},
    {0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00},
    {0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x10,0x00},
    {0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20},
    {0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x03,0x0C,0x30,0x0C,0x03,0x00,0x00},
    {0x80,0x80,0x00,0x80,0x80,0x00,0x80,0x80,0x01,0x0E,0x30,0x0C,0x07,0x38,0x06,0x01},
    {0x00,0x80,0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x20,0x31,0x0E,0x2E,0x31,0x20,0x00},
    {0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x81,0x86,0x78,0x18,0x06,0x01,0x00},
    {0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00}
};

static unsigned char spi_mode=0;/* SPI通信使用全双工，设置CPOL＝0，CPHA＝0。*/
static unsigned char spi_bits=8;/* 8bits读写，MSB first。*/
static unsigned int spi_delay=0;

static int SPI_open(struct tft* config){
    int ret=0;
    static unsigned int spi_speed;
    config->fd=open(config->spidev,O_RDWR);
    if(config->fd<0){
        printf("Can't open device\n");
        ret=-1;
    };
    if(ioctl(config->fd,SPI_IOC_WR_MODE,&spi_mode)<0){
        printf("Can't set spi mode\n");
        ret=-1;
    };
    if(ioctl(config->fd,SPI_IOC_RD_MODE,&spi_mode)<0){
        printf("Can't get spi mode\n");
        ret=-1;
    };
    if(ioctl(config->fd,SPI_IOC_WR_BITS_PER_WORD,&spi_bits)<0){
        printf("Can't set bits per word\n");
        ret=-1;
    };
    if(ioctl(config->fd,SPI_IOC_RD_BITS_PER_WORD,&spi_bits)<0){
        printf("Can't get bits per word\n");
        ret=-1;
    };
    spi_speed=config->speed;
    if(ioctl(config->fd,SPI_IOC_WR_MAX_SPEED_HZ,&spi_speed)<0){
        printf("Can't set max speed hz\n");
        ret=-1;
    };
    if(ioctl(config->fd,SPI_IOC_RD_MAX_SPEED_HZ,&spi_speed)<0){
        printf("Can't get max speed hz\n");
        ret=-1;
    };
    return ret;
}

static int SPI_close(struct tft* config){
    if(config->fd==0){
        return 0;
    };
    close(config->fd);
    config->fd=0;
    return 0;
}

static int SPI_write(struct tft* config,unsigned char* TxBuf,int len){
    int ret=0;
    if(write(config->fd,TxBuf,len)<0){
        printf("SPI Write error\n");
        ret=-1;
    };
    return ret;
}

static void TFT_WriteCmd(struct tft* config,unsigned char cmd){
    digitalWrite(config->dc,0);
    SPI_write(config,&cmd,1);
}

static void TFT_WriteData(struct tft* config,unsigned char data){
    digitalWrite(config->dc,1);
    SPI_write(config,&data,1);
}

static void TFT_WriteData16Bit(struct tft* config,unsigned short data){
    unsigned char t1,t2;
    t1=(unsigned char)(data>>8);
    t2=(unsigned char)(data&0xFF);
    digitalWrite(config->dc,1);
    SPI_write(config,&t1,1);
    SPI_write(config,&t2,1);
}

static void TFT_SetRegion(struct tft* config,unsigned short x_start,unsigned short y_start,unsigned short x_end,unsigned short y_end){
    TFT_WriteCmd(config,0x2A);
    TFT_WriteData16Bit(config,x_start+2);
    TFT_WriteData16Bit(config,x_end+2);
    TFT_WriteCmd(config,0x2B);
    TFT_WriteData16Bit(config,y_start+3);
    TFT_WriteData16Bit(config,y_end+3);
    TFT_WriteCmd(config,0x2C);
}

void TFT_Init(struct tft* config){
    wiringPiSetupPhys();
    SPI_open(config);
    pinMode(config->res,OUTPUT);
    pinMode(config->dc,OUTPUT);
    digitalWrite(config->res,1);

    TFT_Reset(config);
    TFT_WriteCmd(config,0x11);
    delay(120);
    TFT_WriteCmd(config,0xB1);
    TFT_WriteData(config,0x01);
    TFT_WriteData(config,0x2C);
    TFT_WriteData(config,0x2D);
    TFT_WriteCmd(config,0xB2);
    TFT_WriteData(config,0x01);
    TFT_WriteData(config,0x2C);
    TFT_WriteData(config,0x2D);
    TFT_WriteCmd(config,0xB3);
    TFT_WriteData(config,0x01);
    TFT_WriteData(config,0x2C);
    TFT_WriteData(config,0x2D);
    TFT_WriteData(config,0x01);
    TFT_WriteData(config,0x2C);
    TFT_WriteData(config,0x2D);
    TFT_WriteCmd(config,0xB4);
    TFT_WriteData(config,0x07);
    TFT_WriteCmd(config,0xC0);
    TFT_WriteData(config,0xA2);
    TFT_WriteData(config,0x02);
    TFT_WriteData(config,0x84);
    TFT_WriteCmd(config,0xC1);
    TFT_WriteData(config,0xC5);
    TFT_WriteCmd(config,0xC2);
    TFT_WriteData(config,0x0A);
    TFT_WriteData(config,0x00);
    TFT_WriteCmd(config,0xC3);
    TFT_WriteData(config,0x8A);
    TFT_WriteData(config,0x2A);
    TFT_WriteCmd(config,0xC4);
    TFT_WriteData(config,0x8A);
    TFT_WriteData(config,0xEE);
    TFT_WriteCmd(config,0xC5);
    TFT_WriteData(config,0x0E);
    TFT_WriteCmd(config,0x36);
    TFT_WriteData(config,0xC8);
    TFT_WriteCmd(config,0xE0);
    TFT_WriteData(config,0x0F);
    TFT_WriteData(config,0x1A);
    TFT_WriteData(config,0x0F);
    TFT_WriteData(config,0x18);
    TFT_WriteData(config,0x2F);
    TFT_WriteData(config,0x28);
    TFT_WriteData(config,0x20);
    TFT_WriteData(config,0x22);
    TFT_WriteData(config,0x1F);
    TFT_WriteData(config,0x1B);
    TFT_WriteData(config,0x23);
    TFT_WriteData(config,0x37);
    TFT_WriteData(config,0x00);
    TFT_WriteData(config,0x07);
    TFT_WriteData(config,0x02);
    TFT_WriteData(config,0x10);
    TFT_WriteCmd(config,0xE1);
    TFT_WriteData(config,0x0F);
    TFT_WriteData(config,0x1B);
    TFT_WriteData(config,0x0F);
    TFT_WriteData(config,0x17);
    TFT_WriteData(config,0x33);
    TFT_WriteData(config,0x2C);
    TFT_WriteData(config,0x29);
    TFT_WriteData(config,0x2E);
    TFT_WriteData(config,0x30);
    TFT_WriteData(config,0x30);
    TFT_WriteData(config,0x39);
    TFT_WriteData(config,0x3F);
    TFT_WriteData(config,0x00);
    TFT_WriteData(config,0x07);
    TFT_WriteData(config,0x03);
    TFT_WriteData(config,0x10);
    TFT_WriteCmd(config,0x2A);
    TFT_WriteData(config,0x00);
    TFT_WriteData(config,0x00+2);
    TFT_WriteData(config,0x00);
    TFT_WriteData(config,0x80+2);
    TFT_WriteCmd(config,0x2B);
    TFT_WriteData(config,0x00);
    TFT_WriteData(config,0x00+3);
    TFT_WriteData(config,0x00);
    TFT_WriteData(config,0x80+3);
    TFT_WriteCmd(config,0xF0);
    TFT_WriteData(config,0x01);
    TFT_WriteCmd(config,0xF6);
    TFT_WriteData(config,0x00);
    TFT_WriteCmd(config,0x3A);
    TFT_WriteData(config,0x05);
    TFT_WriteCmd(config,0x29);
}

void TFT_Reset(struct tft* config){
    digitalWrite(config->res,0);
    delay(100);
    digitalWrite(config->res,1);
    delay(100);
}

void TFT_Clear(struct tft* config,unsigned short Color){
    int i,j;
    TFT_SetRegion(config,0,0,config->width-1,config->height-1);
    for(i=0;i<config->width;i++){
        for(j=0;j<config->height;j++){
            TFT_WriteData16Bit(config,Color);
        };
    };
}

void TFT_DrawPoint(struct tft* config,unsigned short x,unsigned short y,unsigned short Color){
    TFT_SetRegion(config,x,y,x+1,y+1);
    TFT_WriteData16Bit(config,Color);
}

void TFT_DrawRect(struct tft* config,unsigned short x1,unsigned short y1,unsigned short x2,unsigned short y2,unsigned short Color){
    int i,j;
    TFT_SetRegion(config,x1,y1,x2,y2);
    for(i=0;i<=x2-x1;i++){
        for(j=0;j<=y2-y1;j++){
            TFT_WriteData16Bit(config,Color);
        };
    };
}

void TFT_DrawLine(struct tft* config,unsigned short x1,unsigned short y1,unsigned short x2,unsigned short y2,unsigned short Color){
    int x,y;
    for(x=x1;x<x2;x++){
        y=(y2-y1)/(x2-x1)*(x-x1)+y1;
        TFT_SetRegion(config,x,y,x+1,y+1);
        TFT_WriteData16Bit(config,Color);
    };
}

void TFT_DrawChar(struct tft* config,unsigned short x,unsigned short y,unsigned int ascii_code,unsigned short front_color,unsigned short back_color){
    int i,j;
    unsigned int code=ascii_code-32;
    TFT_SetRegion(config,x,y,x+7,y+15);
    for(i=0;i<8;i++){
        for(j=0;j<8;j++){
            if((F8X16_ASCII[code][j]>>i)&0x01){
                TFT_WriteData16Bit(config,front_color);
            }else{
                TFT_WriteData16Bit(config,back_color);
            };
        };
    };
    for(i=0;i<8;i++){
        for(j=0;j<8;j++){
            if((F8X16_ASCII[code][j+8]>>i)&0x01){
                TFT_WriteData16Bit(config,front_color);
            }else{
                TFT_WriteData16Bit(config,back_color);
            };
        };
    };
}

void TFT_DrawString(struct tft* config,unsigned short x,unsigned short y,char* string,unsigned short front_color,unsigned short back_color){
    int i,len=strlen(string);
    for(i=0;i<len;i++){
        TFT_DrawChar(config,x+8*i,y,*(string+i),front_color,back_color);
    };
}

void TFT_Close(struct tft* config){
    SPI_close(config);
}